package edu.scau.divineplace.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import edu.scau.divineplace.entity.dos.CommentScoreDO;
import edu.scau.divineplace.entity.dos.ContentBasicInformationDO;
import edu.scau.divineplace.entity.dos.UserDO;
import edu.scau.divineplace.entity.query.AccountQuery;
import edu.scau.divineplace.entity.vo.AccountVO;
import edu.scau.divineplace.entity.vo.CommentScoreVO;
import edu.scau.divineplace.util.BeanConvertUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @Author: x1aolone
 * @Date: 2020/12/11 15:48
 */
@Service
public class AccountService {

    @Autowired
    private ImportMapperService importMapperService;


    /**
     * 通过userId获取用户信息
     * @param userId
     * @return
     */
    public AccountVO get(Long userId){
        UserDO DO = importMapperService.userMapper.selectById(userId);
        AccountVO VO = BeanConvertUtils.DO2BO(DO, AccountVO.class);
        return VO;
    }

    public void update(Long userId, AccountQuery query){
        LambdaUpdateWrapper<UserDO> updateWrapper = Wrappers.<UserDO>lambdaUpdate()
                .eq(UserDO::getId, userId);
        UserDO DO = new UserDO();
        BeanUtils.copyProperties(query, DO);
        // entity为null时不会使用自动注入更新updateTime，所以传一个对象进去
        importMapperService.userMapper.update(DO, updateWrapper);
    }

    /**
     * 通过电话号码和密码登录, 若成功登录返回用户信息
     * @param telephone
     * @param password
     * @return
     */
    public AccountVO login(String telephone, String password){
        LambdaQueryWrapper wrapper = Wrappers.<UserDO>lambdaQuery()
                .eq(UserDO::getTelephone, telephone)
                .eq(UserDO::getPassword, password)
                .last("limit 1");
        UserDO DO = importMapperService.userMapper.selectOne(wrapper);
        Preconditions.checkNotNull(DO, "用户名或密码错误");
        AccountVO VO = BeanConvertUtils.DO2BO(DO, AccountVO.class);
        return VO;
    }

    /**
     * 注册, 若注册成功返回用户信息
     * @param query
     * @return
     */
    public AccountVO register(@Validated AccountQuery query){
        UserDO DO = BeanConvertUtils.BO2DO(query, UserDO.class);
        Preconditions.checkArgument(!telephoneExist(DO.getTelephone()), "该手机号已被注册过");
        Preconditions.checkArgument(!nameExist(DO.getName()), "该用户名已被注册过");
        DO.setAvatar("default_avatar");
        importMapperService.userMapper.insert(DO);
        AccountVO VO = BeanConvertUtils.DO2BO(DO, AccountVO.class);
        return VO;
    }

    public boolean telephoneExist (String telephone) {
        LambdaQueryWrapper wrapper = Wrappers.<UserDO>lambdaQuery()
                .eq(UserDO::getTelephone, telephone);
        int count = importMapperService.userMapper.selectCount(wrapper);
        return count > 0;
    }

    public boolean nameExist (String username) {
        LambdaQueryWrapper wrapper = Wrappers.<UserDO>lambdaQuery()
                .eq(UserDO::getName, username);
        int count = importMapperService.userMapper.selectCount(wrapper);
        return count > 0;
    }

}
